package pers.books.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import pers.books.entity.User;
import pers.books.entity.UserBook;
import pers.books.service.UserBookService;
import pers.books.util.ResultMessage;

/**
 * 
 * UserBooksController
 *
 * @author 姜治昊
 * @time 2018年5月5日 下午6:27:30
 */
@RestController
@RequestMapping("user/book")
public class UserBooksController {

	@Autowired
	private UserBookService userBookSerivce;
	
	/**
	 * 我的图书列表
	 * @param session
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(value="getbooks", method=RequestMethod.POST)
	public Map<String, Object> getBooks(HttpSession session, int limit, int offset
			, @RequestParam(value="bookName", required=false, defaultValue="") String bookName
			, @RequestParam(value="isbn", required=false, defaultValue="") String isbn
			, @RequestParam(value="site", required=false, defaultValue="") String site) {
//		User user = (User) session.getAttribute(StaticMessage.LOGIN_USER);
		Map<String, Object> result = userBookSerivce
				.getBooks("ac796ae452104bf5b5d192bd5acc64b8", "%"+bookName+"%", "%"+isbn+"%", "%"+site+"%", offset, limit);
		return result;
	}
	
	
	/**
	 * 添加我的图书
	 * @param session
	 * @param userBook
	 * @return
	 */
	@RequestMapping(value="addBook", method=RequestMethod.POST)
	public ResultMessage<?> addBook(HttpSession session, UserBook userBook) {
		userBook.setUser(new User("ac796ae452104bf5b5d192bd5acc64b8"));
		return userBookSerivce.addMyBook(userBook);
//		userBook.setUser(new User(((User) session.getAttribute(StaticMessage.LOGIN_USER)).getId()));
		
		
	}
	
}
